<template>
  <div class="p-2">

    <div class="mb-[10px]">
      <el-card>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          label-width="auto"
        >
          <el-form-item
            label="运营商名称"
            prop="name"
          >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入运营商名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="运营商编号"
            prop="no"
          >
            <el-input
              v-model="queryParams.no"
              placeholder="请输入运营商编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleQuery"
              icon="Search"
            >查询</el-button>
            <el-button
              @click="resetQuery"
              icon="Refresh"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <template
        v-if="type!='product'"
        #header
      >
        <el-row :gutter="10"  v-hasPermi="['payment:operator:add']">
          <el-col :span="1.5">
            <el-button
              type="primary"
              @click="addAndEditOperator('add')"
              icon="Plus"
            >新增</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table
        v-loading="loading"
        :data="operatorList"
        border
      >
        <el-table-column
          label="创建时间"
          align="center"
          min-width="160"
          prop="createTime"
          sortable
        />
        <el-table-column
          label="运营商名称"
          align="center"
          min-width="160"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="运营商编号"
          align="center"
          min-width="160"
          prop="no"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="法人信息"
          align="center"
          min-width="240"
        >
          <template #default="scope">
            <div v-if="scope.row.fr">姓名：{{ scope.row.fr }}</div>
            <div v-if="scope.row.frSfz">身份证：{{ scope.row.frSfz }}</div>
            <div v-if="scope.row.frSjh">手机号：{{ scope.row.frSjh }}</div>
          </template>
        </el-table-column>
        <el-table-column
          label="经办人信息"
          align="center"
          min-width="240"
        >
          <template #default="scope">
            <div v-if="scope.row.wtr">姓名：{{ scope.row.wtr }}</div>
            <div v-if="scope.row.wtrSfz">身份证：{{ scope.row.wtrSfz }}</div>
            <div v-if="scope.row.wtrSjh">手机号：{{ scope.row.wtrSjh }}</div>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          width="120"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tag
              v-if="type!='product'"
              v-hasPermi="['payment:operator:edit']"
              @click="addAndEditOperator('edit',scope.row)"
            >编辑</el-tag>
            <el-tag
              v-if="type!='product'"
              v-hasPermi="['payment:operator:query']"
              @click="addAndEditOperator('look',scope.row)"
            >详情</el-tag>
            <el-tag
              v-if="type!='product'"
              @click="virtualAccountFun(scope.row)"
              v-hasPermi="['payment:operator:addXnAcc']"
            >虚拟账户</el-tag>
            <el-tag
              v-if="type=='product'"
              type="success"
              @click="productConfig(scope.row)"
            >产品配置</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <VirtualAccount ref="virtualAccountRef"></VirtualAccount>
  </div>
</template>

<script setup name="operatorTable" lang="ts">
import api from '@/api/operator/addOperator';
import { OperatorQuery, OperatorVO } from '@/api/operator/addOperator/types';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps({
  type: {
    type: String,
    default: ''
  }
});

const operatorList = ref<OperatorVO[]>();
const loading = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const queryParams = ref<OperatorQuery>({
  pageNum: 1,
  pageSize: 10,
  name: '',
  no: ''
});

/** 查询运营商列表 */
const getList = async () => {
  loading.value = true;
  const res = await api.operatorList(proxy?.addDateRange(queryParams.value, dateRange.value));
  loading.value = false;
  operatorList.value = res.rows;
  total.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  handleQuery();
};

/** 新增&修改 */
const addAndEditOperator = (type: string, row: any = '') => {
  router.push(type == 'look' ? 'operatorDetail?id=' + row.id : type == 'edit' ? 'operatorEdit?id=' + row.id : 'operatorAdd');
};
/** 虚拟账户 */
const virtualAccountRef = ref<any>();
const virtualAccountFun = (row: any) => {
  virtualAccountRef.value.virtualAccountVisibleFun('operator', row);
};
/** 产品配置 */
const productConfig = (row: any) => {
  router.push('operatorProductConfig?id=' + row.id + '&no=' + row.no + '&name=' + row.name);
};

onMounted(() => {
  getList(); // 初始化列表数据
});
</script>

<style lang="scss" scoped></style>
